﻿//---------------------------------------------------------------------------
//
// Copyright (C) Inter Access B.V..  All rights reserved.
//
//---------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Schedule.Primitives;

namespace Schedule
{
    public class ScheduleFlatView : Control
    {
        static ScheduleFlatView()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ScheduleFlatView), new FrameworkPropertyMetadata(typeof(ScheduleFlatView)));
        }

        public ScheduleFlatView()
        {

        }

        #region Dependency Properties

        /// <summary>
        /// Date to display
        /// </summary>
        public DateTime Date
        {
            get { return (DateTime)GetValue(DateProperty); }
            set { SetValue(DateProperty, value); }
        }

        public static readonly DependencyProperty DateProperty =
            DependencyProperty.Register("Date", typeof(DateTime), typeof(ScheduleFlatView), new UIPropertyMetadata(OnDatePropertyChanged));

        private static void OnDatePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            ScheduleFlatView presenter = (ScheduleFlatView)sender;
            //presenter.RebuildCells();
        }

        /// <summary>
        /// Appointments to display
        /// </summary>
        public ObservableCollection<Appointment> Appointments
        {
            get { return (ObservableCollection<Appointment>)GetValue(AppointmentsProperty); }
            set { SetValue(AppointmentsProperty, value); }
        }

        public static readonly DependencyProperty AppointmentsProperty =
            DependencyProperty.Register("Appointments", typeof(ObservableCollection<Appointment>), typeof(ScheduleFlatView), new UIPropertyMetadata());

        #endregion

        #region Overrides

        public override void OnApplyTemplate()
        {
            // Create and set a filter on the appointment
            // We do not need one for cells, because we create it ourselves
            ScheduleFlatItemPresenter appointments = (ScheduleFlatItemPresenter)this.GetTemplateChild("PART_Appointments");
            CollectionViewSource appointmentsResource = (CollectionViewSource)appointments.FindResource("Appointments");
            appointmentsResource.Filter += new FilterEventHandler(DateTimeFilter);

            base.OnApplyTemplate();
        }

        /// <summary>
        /// Filters the appointments in the collection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DateTimeFilter(object sender, FilterEventArgs e)
        {
            DateTime start;
            DateTime end;

            // Change this into something more generic
            if (e.Item is Appointment)
            {
                Appointment item = (Appointment)e.Item;
                start = item.Start;
                end = item.End;

                bool bool1 = start < this.Date.AddDays(1);
                bool bool2 = end > this.Date;
                bool bool3 = (end - start).TotalHours > 48;

                if (bool1 && bool2 && bool3)
                {
                    e.Accepted = true;
                    return;
                }
            }
            e.Accepted = false;
        }

        #endregion
    }
}
